Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Firestore Swift Cpp Experiment #13956

Open
wants to merge 25 commits into
base: main
Choose a base branch
from
Open

Conversation

cherylEnkidu
Copy link
Contributor

@cherylEnkidu cherylEnkidu commented Oct 23, 2024

Here are some imperfections in this PR. Please feel free to suggest better solutions:

  1. Exposure of C++ APIs in the Objective-C Interface
    The Swift layer can only access public APIs of Objective-C. In Swift Package Manager, only a single path can be designated as the public header path. To enable additional features in the Swift layer, it must access internal C++ members held by the Objective-C object.

  2. Swift's async/await functionality requires Objective-C callbacks for implementation. As a result, a public API—used exclusively by the Swift layer—was introduced. (Potentially this can be avoid using a C++ class contains Swift member, since the current structure only supports one way dependency, Swift class has C++ member. This impl is listed as a future improvement)

  3. The FirebaseFirestoreCpp binary build structure in Swift Package Manager is currently written as pseudo-code and has not been verified.

  4. The include path in this PR using relative path instead of full path, since full path leads to build failure for unknown reason.

@google-oss-bot
Copy link

google-oss-bot commented Oct 23, 2024

1 Warning
⚠️ Did you forget to add a changelog entry? (Add #no-changelog to the PR description to silence this warning.)

Generated by 🚫 Danger

Copy link
Contributor

github-actions bot commented Oct 23, 2024

Apple API Diff Report

Commit: 732555b
Last updated: Thu Jan 9 11:25 PST 2025
View workflow logs & download artifacts


[BUILD ERROR] FirebaseFirestore


FirebaseCore

Functions

[MODIFIED] FIRFirebaseVersion
[MODIFIED] FIRFirebaseVersion
Swift:
+  func FirebaseVersion () -> String
-  func FirebaseVersion () -> String
Objective-C:
+  NS_SWIFT_NAME ( FirebaseVersion ()) NSString * FIRFirebaseVersion ( void )
-  NSString * _Nonnull FIRFirebaseVersion ( void )

[BUILD ERROR] FirebaseFirestoreInternal


s.public_header_files = 'FirebaseFirestoreInternal/**/*.h'
s.public_header_files = [
'FirebaseFirestoreInternal/**/*.h',
'Firestore/Swift/Source/SwiftAPI/*.swift',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Swift files should not be in the headers

Copy link
Member

@paulb777 paulb777 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I only see relative paths in public headers which is required. Is there a place where relative headers are used other than in a public header?

@@ -0,0 +1,43 @@
// swift-tools-version:5.9
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought setting swift-tools-version is only meaningful for Package.swift files? What does it mean here?

@discardableResult
public func GetPipelineResult() async throws -> [PipelineResult] {
return try await withCheckedThrowingContinuation { continuation in
let listener = CallbackWrapper.wrapPipelineCallback(firestore: cppObj.GetFirestore()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What needs to be done to fix the CII issues highlighted below?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like the CI cannot pick up the changes in the public Objective-C API, not sure why ...

@@ -1392,6 +1398,52 @@ func firestoreWrapperTarget() -> Target {
)
}

func firebaseFirestoreCppTarget() -> Target {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why an additional target versus modifying the existing Firestore target?

Will pipelines be an optional feature to be configured into the SDK?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Firestore currently has three public folders for C++, Objective-C, and Swift.

Each build target can only specify a single path for the public header folder.

Ideally, the structure should follow the pattern: Swift wrapping Objective-C, which wraps C++. However, reorganizing the existing structure to achieve this is complex. Additionally, future improvements can still be made without requiring changes to the public API. For these reasons, I have chosen to add another target as a temporary workaround.

Comment on lines 50 to 53
+ (std::unique_ptr<core::EventListener<std::vector<api::PipelineResult>>>)
wrapPipelineCallback:(std::shared_ptr<api::Firestore>)firestore
completion:(void (^)(std::shared_ptr<std::vector<api::PipelineResult>> result,
NSError *_Nullable error))completion
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this intended to be exposed to customers? And should it use the ObjC types for it's parameters instead of C++ ones? e.g. FIRFirestore over std::shared_ptr<api::Firestore>

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason for using C++ types instead of Objective-C is that this class is intended to be used exclusively internally by the Swift layer and not by developers. The goal is to minimize the use of Objective-C wherever possible.


+ (std::unique_ptr<core::EventListener<std::vector<api::PipelineResult>>>)
wrapPipelineCallback:(std::shared_ptr<api::Firestore>)firestore
completion:(void (^)(std::shared_ptr<std::vector<api::PipelineResult>> result,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a newer learning, but it seems like making the callbacks in callback-based APIs Sendable so they can be invoked on any thread can prevent crashes when they do get called on a different thread. If this callback may be called on a different thread than the one it was defined in, then we should mark it as Sendable with.

Suggested change
completion:(void (^)(std::shared_ptr<std::vector<api::PipelineResult>> result,
completion:(void (^NS_SWIFT_SENDABLE)(std::shared_ptr<std::vector<api::PipelineResult>> result,

@cherylEnkidu
Copy link
Contributor Author

cherylEnkidu commented Jan 8, 2025

I only see relative paths in public headers which is required. Is there a place where relative headers are used other than in a public header?

Aw I see, so public headers can only used relative path?

The reason I am asking is that I cannot include existing files from the previous API folder, Firestore/core/src/api. For example, I am unable to add Firestore/core/src/api/aggregate_query.h. Even when using a relative path for aggregate_query, the file contains other includes with full paths that Swift Package Manager cannot resolve.

Also, use relative path would fail syntax check: Firestore/core/interfaceForSwift/api/collection_stage.h:19: Include the directory when naming .h files [build/include_subdir]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants